package com.zyz.rabbitextensionsdemo.config;

import com.zyz.rabbitextensionsdemo.constants.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    // 消息确认
    @Bean("ackQueue")
    public Queue ackQueue() {
        return QueueBuilder.durable(Constants.ACK_QUEUE).build();
    }
    @Bean("ackExchange")
    public DirectExchange ackExchange() {
        return ExchangeBuilder.directExchange(Constants.ACK_EXCHANGE).build();
    }
    @Bean("ackBinding")
    public Binding ackBinding(@Qualifier("ackExchange") DirectExchange directExchange,@Qualifier("ackQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("ack");
    }
    // 发送确认
    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(Constants.CONFIRM_QUEUE).build();
    }
    @Bean("confirmExchange")
    public DirectExchange confirmExchange() {
        return ExchangeBuilder.directExchange(Constants.CONFIRM_EXCHANGE).build();
    }
    @Bean("confirmBinding")
    public Binding confirmBinding(@Qualifier("confirmExchange") DirectExchange directExchange,@Qualifier("confirmQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("confirm");
    }
    // 重试机制
    @Bean("retryQueue")
    public Queue retryQueue() {
        return QueueBuilder.durable(Constants.RETRY_QUEUE).build();
    }
    @Bean("retryExchange")
    public DirectExchange retryExchange() {
        return ExchangeBuilder.directExchange(Constants.RETRY_EXCHANGE).build();
    }
    @Bean("retryBinding")
    public Binding retryBinding(@Qualifier("retryExchange") DirectExchange directExchange, @Qualifier("retryQueue") Queue queue){
        return BindingBuilder.bind(queue).to(directExchange).with("retry");
    }
    // TTL
    // 未设置ttl队列
    @Bean("ttlQueue")
    public Queue ttlQueue() {
        return QueueBuilder.durable(Constants.TTL_QUEUE).build();
    }
    // 设置ttl队列
    @Bean("ttlQueue2")
    public Queue ttlQueue2() {
        return QueueBuilder.durable(Constants.TTL_QUEUE2).ttl(20_000).build(); // 20s
    }
    @Bean("ttlExchange")
    public DirectExchange ttlExchange() {
        return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).build();
    }
    @Bean("ttlBinding")
    public Binding ttlBinding(@Qualifier("ttlExchange") DirectExchange directExchange,@Qualifier("ttlQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("ttl");
    }
    @Bean("ttlBinding2")
    public Binding ttlBinding2(@Qualifier("ttlExchange") DirectExchange directExchange,@Qualifier("ttlQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("ttl");
    }
    // 死信
    // 正常交换机和队列
    @Bean("normalQueue")
    public Queue normalQueue() {
        return QueueBuilder.durable(Constants.NORMAL_QUEUE)
                        .deadLetterExchange(Constants.DL_EXCHANGE)// 配置和死信交换机的关联
                        .deadLetterRoutingKey("dl")          // 配置和死信交换机的RoutingKey
                        .ttl(10000)
                        .maxLength(10)                      // 队列长度
                        .build();
    }
    @Bean("normalExchange")
    public DirectExchange normalExchange() {
        return ExchangeBuilder.directExchange(Constants.NORMAL_EXCHANGE).build();
    }
    @Bean("normalBinding")
    public Binding normalBinding(@Qualifier("normalExchange") DirectExchange directExchange,@Qualifier("normalQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("normal");
    }
    // 死信交换机和队列
    @Bean("dlQueue")
    public Queue dlQueue() {
        return QueueBuilder.durable(Constants.DL_QUEUE).build();
    }
    @Bean("dlExchange")
    public DirectExchange dlExchange() {
        return ExchangeBuilder.directExchange(Constants.DL_EXCHANGE).build();
    }
    @Bean("dlBinding")
    public Binding dlBinding(@Qualifier("dlExchange") DirectExchange directExchange,@Qualifier("dlQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("dl");
    }
    // 事务
    @Bean("transactionQueue")
    public Queue transactionQueue() {
        return QueueBuilder.durable(Constants.TRANSACTION_QUEUE).build();
    }
    @Bean("transactionExchange")
    public DirectExchange transactionExchange() {
        return ExchangeBuilder.directExchange(Constants.TRANSACTION_EXCHANGE).build();
    }
    @Bean("transactionBinding")
    public Binding transactionBinding(@Qualifier("transactionExchange") DirectExchange directExchange,@Qualifier("transactionQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("transaction");
    }
}
